package StudyCode.summary.mycollection;

import java.util.Arrays;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class MoveObjectUpInAList {
	public static void main(String[] argv) {
		List list = Arrays.asList("科技与人文", "www.tah1986.com");
		Object o = "www.tah1986.com";
		System.out.println("移动前，List:" + list);
		moveUp(list, o);
		System.out.println("移动后，List:" + list);
	}

	@SuppressWarnings("unchecked")
	public static boolean moveUp(List list, Object o) {
		int index = list.indexOf(o);
		if (index >= 0) {
			try {
				Object temp = list.set(index - 1, o);
				list.set(index, temp);
				return true;
			} catch (IndexOutOfBoundsException e) {
				return false;
			}
		}
		return false;
	}
}
